from typing import List
from fastapi import APIRouter,HTTPException,status

from database.model import Chapter, Question,SheetItem
from .model import ChapterProgressModel, SubjectProgressModel, YearProgressModel

router=APIRouter(tags=['progress'],prefix='/progress')

@router.get('/year/{year}',response_model=YearProgressModel)
def get_year_progress(year:int):
    total_count=Question.select().where(Question.year==year).count()
    if total_count==0:
        return YearProgressModel(title=str(year),key=year)
    undo_count=SheetItem.select()\
        .join(Question)\
        .where(Question.year==year)\
        .where(SheetItem.check_count==0)\
        .count()
    right_count=SheetItem.select()\
        .join(Question)\
        .where(Question.year==year)\
        .where(SheetItem.last_check_right==True)\
        .count()
    wrong_count=total_count-undo_count-right_count
    return YearProgressModel(
        title=str(year),
        key=year,
        total_count=total_count,
        undo_percent=undo_count/total_count,
        wrong_percent=wrong_count/total_count,
        right_percent=right_count/total_count)

@router.get('/years',response_model=List[YearProgressModel])
def get_years_progress():
    years=[q.year for q in Question.select(Question.year).distinct()]
    res=list()
    for year in sorted(years):
        res.append(get_year_progress(year))
    return res 

@router.get('/subject/{subject_id}',response_model=SubjectProgressModel)
def get_subject_progress(subject_id:int):
    subject=Chapter.get_by_id(subject_id)
    total_count=Question.select().where(Question.subject==subject).count()
    if total_count==0:
        return SubjectProgressModel(key=subject_id,title=subject.title)
    undo_count=SheetItem.select()\
        .join(Question)\
        .where(Question.subject==subject)\
        .where(SheetItem.check_count==0)\
        .count()
    right_count=SheetItem.select()\
        .join(Question)\
        .where(Question.subject==subject)\
        .where(SheetItem.last_check_right==True)\
        .count()
    wrong_count=total_count-undo_count-right_count
    return SubjectProgressModel(
        key=subject_id,
        title=subject.title,
        total_count=total_count,
        undo_percent=undo_count/total_count,
        wrong_percent=wrong_count/total_count,
        right_percent=right_count/total_count)

@router.get('/subjects',response_model=List[SubjectProgressModel])
def get_subjects_progress():
    subjects_id=(subject.id for subject in Chapter.select(Chapter.id).where(Chapter.id<1000))
    res=list()
    for sid in sorted(subjects_id):
        res.append(get_subject_progress(sid))
    return res

@router.get('/chapter/{chapter_id}')
def get_chapter_progress(chapter_id:int):
    if chapter_id<1000:
        raise HTTPException(status_code=404,detail='Chater id must be great than 1000!')
    chapter=Chapter.get_by_id(chapter_id)
    total_count=Question.select().where(Question.chapter==chapter).count()
    if total_count==0:
        return ChapterProgressModel(key=chapter_id,title=chapter.title)
    undo_count=SheetItem.select()\
        .join(Question)\
        .where(Question.chapter==chapter)\
        .where(SheetItem.check_count==0)\
        .count()
    right_count=SheetItem.select()\
        .join(Question)\
        .where(Question.chapter==chapter)\
        .where(SheetItem.last_check_right==True)\
        .count()
    wrong_count=total_count-undo_count-right_count
    return ChapterProgressModel(
        key=chapter_id,
        title=chapter.title,
        total_count=total_count,
        undo_percent=undo_count/total_count,
        wrong_percent=wrong_count/total_count,
        right_percent=right_count/total_count)

@router.get('/chapters',response_model=List[ChapterProgressModel])
def get_chapters_progress_by_subject(subject_id:int):
    chapters_id=(ch.id for ch in Chapter.select(Chapter.id).where(Chapter.parent==subject_id))
    res=[]
    for cid in chapters_id:
        res.append(get_chapter_progress(cid))
    return res